module.exports = function socket() { // 用于UDP连接

    const dgram = require('dgram');
    const server = dgram.createSocket('udp4');

    server.on('error', (err) => {
        console.log(`服务器异常：\n${err.stack}`);
        server.close();
    });

    server.on('message', (msg, rinfo) => {
        console.log(`服务器接收到来自 ${rinfo.address}:${rinfo.port} 的 ${msg}`);
    });

    server.on('listening', () => {
        const address = server.address();
        console.log(`服务器监听 ${address.address}:${address.port}`);
    });

    server.bind(1234);

    const server1 = dgram.createSocket('udp4');

    server1.on('error', (err) => {
        console.log(`服务器1异常：\n${err.stack}`);
        server1.close();
    });

    server1.on('message', (msg, rinfo) => {
        console.log(`服务器1接收到来自 ${rinfo.address}:${rinfo.port} 的 ${msg}`);
    });

    server1.on('listening', () => {
        const address = server.address();
        console.log(`服务器1监听 ${address.address}:${address.port}`);
    });

    server1.bind(4321);
}